package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.AddressDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Address;
import com.itheima.pojo.CheckItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.JedisPool;

import java.util.List;

/**
 * 体检机制地址服务
 */

@Service(interfaceClass = AddressService.class)
@Transactional
public class AddressServiceImpl implements AddressService {
    @Autowired
    private AddressDao addressDao;
    @Autowired
    private JedisPool jedisPool;

    //新增体检地址
    public void add(Address address) {
        addressDao.add(address);
    }

    //分页查询
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();//ThreadLocal
        PageHelper.startPage(currentPage, pageSize);//分页插件，会在执行sql之前将分页关键字追加到SQL后面
        Page<CheckItem> page = addressDao.findByCondition(queryString);
        return new PageResult(page.getTotal(), page.getResult());
    }

    //根据id 查询体检地址
    public Address findById(Integer id) {
        return addressDao.findById(id);
    }

    //编辑体检地址
    public void edit(Address address) {
        addressDao.edit(address);
    }

    //删除功能
    public void delete(Integer id) {
        List<Integer> list = addressDao.findAddressAndOrderByAid(id);
        if (list == null) {
            addressDao.delAddress(id);
        } else {
            addressDao.delAddressAndOrder(id);
            addressDao.delAddress(id);
        }
    }

    //查询所有地址
    public List<Address> findAll() {
        return addressDao.findAll();
    }

}
